﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace Alfa.Common.ValueConverters {
	/// <summary>
	/// Value converter that translates <see cref="Decimal"/> to string.
	/// </summary>
	public sealed class DecimalToStringConverter : IValueConverter {
		/// <summary>
		/// Converts <see cref="Decimal"/> value to string.
		/// </summary>
		/// <param name="value">Value to convert. Should be <see cref="Decimal"/>.</param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>Converted value (String)</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value is decimal) {
				return ((decimal)value).ToString( );
			}
			return null;
		}

		/// <summary>
		/// Converts back string value to <see cref="Decimal"/>.
		/// </summary>
		/// <param name="value">Value to convert. Should be string.</param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>Converted value (Decimal)</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value is string) {
				decimal result;
				if (decimal.TryParse((string)value, out result))
					return result;
			}
			return 0;
		}
	}
}
